package com.zytech.zspider.engine;

import com.google.common.base.Objects;
import com.zytech.modules.lang.Requires;

import java.io.Serializable;

/**
 * @author siuming
 */
public class SpiderProxy implements Serializable {

    private static final long serialVersionUID = -2538701048068029403L;
    private String schema;
    private String host;
    private int port;

    /**
     * @param schema
     * @param host
     * @param port
     */
    SpiderProxy(String schema, String host, int port) {
        this.schema = schema;
        this.host = host;
        this.port = port;
    }

    /**
     * @return
     */
    public String getSchema() {
        return schema;
    }

    /**
     * @return
     */
    public String getHost() {
        return host;
    }

    /**
     * @return
     */
    public int getPort() {
        return port;
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(getSchema(), getHost(), getPort());
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (null == obj || getClass() != obj.getClass()) {
            return false;
        }

        SpiderProxy other = (SpiderProxy) obj;
        return Objects.equal(getSchema(), other.getHost()) &&
                Objects.equal(getHost(), other.getHost()) &&
                Objects.equal(getPort(), other.getPort());
    }

    @Override
    public String toString() {
        return String.format("%s:%s:%s", getSchema(), getHost(), getPort());
    }

    /**
     * @param schema
     * @param host
     * @param port
     * @return
     */
    public static SpiderProxy of(String schema, String host, int port) {
        Requires.hasText(schema, "schema must not be null or empty.");
        Requires.hasText(host, "host must not be null or empty.");
        Requires.isTrue(port > 0, "port must gt 0.");
        return new SpiderProxy(schema, host, port);
    }
}
